/**
* @author Francesco di Dio
* Date: 21/apr/2013
* Titolo: DataFileChooserDialog.java
* Versione: 
*/


/**
 * Copyright (c) 2013 Francesco di Dio.
 * tabuto83@gmail.com 
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tabuto.jkeys.views;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.WindowConstants;

import com.tabuto.jkeys.JKUtils;
import com.tabuto.jkeys.JKeys;
import com.tabuto.jkeys.Preferences;

/**
 * 
 */
public class DataFileChooserDialog extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8535940779531053196L;
	private JTextField dataFileField;
	
	public DataFileChooserDialog() {
		
		JLabel lblDataFile = new JLabel("Data File");
		
		dataFileField = new JTextField();
		dataFileField.setColumns(10);
		dataFileField.setText(Preferences.getProperties("conf.data_file_path")); 
		dataFileField.setEditable(false);
		
		JButton btnBrowse = new JButton("Browse");
		
		btnBrowse.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				File f;
	            JFileChooser fileChooser = new JFileChooser();
	            f = new File("JKPassword.dat");
	            fileChooser.setCurrentDirectory(f);
	            fileChooser.setSelectedFile(f);
	            int n = fileChooser.showSaveDialog(getParent());
	            if (n == JFileChooser.APPROVE_OPTION)
	            {
	            	dataFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
	            	
	           // 	if(!f.exists()){
	            		
		         //   	try {
		         //   			f= new File(fileChooser.getSelectedFile().getAbsolutePath());  
		         //   			f.createNewFile();
		            			
				//			} 
		        //    	catch (IOException e1) {
								// TODO Auto-generated catch block
				//				e1.printStackTrace();
				//			}
		           // 	}
	            
	            	
	            	//
//	            	 FileOutputStream fos = null;
//	                 ObjectOutputStream out = null;
//	                 
//	                 fos = new FileOutputStream(fileChooser.getSelectedFile().getAbsolutePath());
//	                 out = new ObjectOutputStream(fos);
//	                 out.writeObject( root );
//	                 out.close();
				
	            }
			}
		});
		
		JButton btnOk = new JButton("OK");
		
		btnOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!JKUtils.isEmptyOrNullString(dataFileField.getText()) ){
					
					Preferences.setProperties("conf.data_file_path", dataFileField.getText());
					closeFrame();
					JKeys.run();
				}else{
					JOptionPane.showMessageDialog(getParent(), "A data file is required!");
				}
				
			}
		});
		
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblDataFile)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(dataFileField, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(btnBrowse)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnOk)
					.addContainerGap(62, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(83)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnBrowse)
							.addComponent(btnOk))
						.addComponent(dataFileField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblDataFile))
					.addContainerGap(154, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		
		Dimension d = new Dimension(500,500);
		setSize(d.width, d.height);
		setPreferredSize(d);
		setResizable(true);
		setVisible(true);
				// Chiudo la finesttra al click della 'X'
				this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	
	public void closeFrame(){
		this.dispose();
	}


}
